<?php

require_once dirname(__FILE__) . '/../lib/clientesGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/clientesGeneratorHelper.class.php';

/**
 * clientes actions.
 *
 * @package    tercermilenio
 * @subpackage clientes
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class clientesActions extends autoClientesActions {

    protected function processForm(sfWebRequest $request, sfForm $form) {

        $formParameters = $request->getParameter($form->getName());

        $formTelefono = $formParameters['cli_telefono'];
        $formCelular = $formParameters['cli_celular'];



        $form->bind($formParameters, $request->getFiles($form->getName()));
        if ($form->isValid()){
            
            if (empty($formTelefono) && empty($formCelular)) {
                $this->getUser()->setFlash('error', 'Debe completar el teléfono o el celular.');
            } else {
                $formValues = $form->getValues();

                $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

                if ($form->getObject()->isNew()) {
                    $existe = ClientesPeer::clienteExiste($form->getValue('cli_nro_documento'));
                } else {
                    $existe = ClientesPeer::clienteExiste($form->getValue('cli_nro_documento'), $form->getValue('cli_id'));
                }

                if (!$existe) {

                    $clientes = $form->save();

                    $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $clientes)));

                    if ($request->hasParameter('_save_and_add')) {
                        $this->getUser()->setFlash('notice', $notice . ' You can add another one below.');

                        $this->redirect('@clientes_new');
                    } else {
                        $this->getUser()->setFlash('notice', $notice);

                        $this->redirect(array('sf_route' => 'clientes_edit', 'sf_subject' => $clientes));
                    }
                } else {
                    $this->getUser()->setFlash('error', 'El documento ingresado ya existe. Por favor, ingrese uno distinto.', false);
                    //$this->redirect(array('sf_route' => 'proveedores_edit', 'sf_subject' => $proveedores));
                }
            }
        } else {
            $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
        }
    }

    public function executeBackToMenu() {
        $this->redirect('@clientes');
    }

    public function executeLocalidadesPorProvincia(sfWebRequest $request) {

        $localidades = null;
        $this->pciaId = $request->getParameter('pcia_id');
        $this->locId = $request->getParameter('loc_id');

        if (!empty($this->pciaId)) {

            $localidades = LocalidadesPeer::getLocalidadesPorProvincia($this->pciaId);
        } else {
            $localidades = LocalidadesPeer::doSelect(new Criteria());
        }
        $this->localidades = $localidades;
    }

    public function executeActivar(sfWebRequest $request) {
        $cliId = $request->getParameter('cli_id');
        try {
            $cliente = ClientesPeer::retrieveByPK($cliId);
            if (empty($cliente)) {
                throw new Exception('El cliente no existe.');
            }
            $activeState = 1;

            $cliente->setCliEstado($activeState);
            $cliente->save();
        } catch (sfException $e) {
            sfContext::getInstance()->getUser()->setFlash('error', 'El cliente no existe.');
        }
        $this->redirect('@clientes');
    }

    public function executeDesactivar(sfWebRequest $request) {
        $cliId = $request->getParameter('cli_id');
        try {
            $cliente = ClientesPeer::retrieveByPK($cliId);

            $inactiveState = 0;
            $cliente->setCliEstado($inactiveState);
            $cliente->save();
        } catch (sfException $e) {
            sfContext::getInstance()->getUser()->setFlash('error', 'El usuario no existe.');
        }
        $this->redirect('@clientes');
    }

    public function executeObtenerProvincia(sfWebRequest $request) {
        if ($request->hasParameter('loc_id')) {
            $this->loc_id = $request->getParameter('loc_id');
        }

        $this->localidad = LocalidadesPeer::retrieveByPk($this->loc_id);

        $this->provincias = ProvinciasPeer::doSelect(new Criteria());
        if (!empty($this->localidad)) {
            $this->provincia = ProvinciasPeer::retrieveByPk($this->localidad->getPciaId());
        }
    }

}
